package com.efast.dmom.aps.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.bean.SupplierPart;
import com.efast.dmom.aps.datahandlers.SupplierPartDataHandler;
import com.efast.dmom.aps.exception.APSException;
import com.efast.dmom.aps.service.SupplierPartService;

@Service
public class SupplierPartServiceImpl implements SupplierPartService {
	@Autowired
	private SupplierPartDataHandler supplierPartDataHandler;

//	@Override
	public ResultBean save(SupplierPart supplierPart) throws APSException {
		ResultBean resultBean = new ResultBean(true, ' ');
		// check required items
		if (!StringUtil.isNotBlank(supplierPart.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查名称是否填写!");
			return resultBean;
		}
		if (supplierPart.getInnerData().getPartId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查物料是否填写!");
			return resultBean;
		}
		if (supplierPart.getInnerData().getSupplierId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查供应商是否填写!");
			return resultBean;
		}
		SupplierPart supplierPartByName = supplierPartDataHandler.getByName(supplierPart.getInnerData().getName());
		if (supplierPartByName.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("名称已存在，请重填!");
			return resultBean;
		}
		SupplierPart SupplierPartData = supplierPartDataHandler.getCustomerPartBySupplierIdAndPartId(
				supplierPart.getInnerData().getSupplierId(), supplierPart.getInnerData().getPartId());
		if (SupplierPartData.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("供应商物料关系已存在，请重填!");
			return resultBean;
		}
		supplierPartDataHandler.save(supplierPart);
		resultBean.setSuccess(true);
		resultBean.setMessage("供应商物料关系保存成功!");
		return resultBean;
	}

//	@Override
	public ResultBean delete(SupplierPart supplierPart) {
		ResultBean resultBean = new ResultBean(true, ' ');
		supplierPartDataHandler.delete(supplierPart);
		resultBean.setSuccess(true);
		resultBean.setMessage("供应商物料关系删除成功!");
		return resultBean;
	}

//	@Override
	public ResultBean update(SupplierPart supplierPart) {
		ResultBean resultBean = new ResultBean(true, ' ');
		// check required items
		if (!StringUtil.isNotBlank(supplierPart.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查名称是否填写!");
			return resultBean;
		}
		if (supplierPart.getInnerData().getPartId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查物料是否填写!");
			return resultBean;
		}
		if (supplierPart.getInnerData().getSupplierId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查供应商是否填写!");
			return resultBean;
		}
		SupplierPart supplierPartById = supplierPartDataHandler.getById(supplierPart.getInnerData().getId());
		if (supplierPartById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的数据不存在!");
			return resultBean;
		}
		if (! supplierPart.getInnerData().getName().equals(supplierPartById.getInnerData().getName())) {
				resultBean.setSuccess(false);
				resultBean.setMessage("名称不可修改!");
				return resultBean;
		}
		if (supplierPart.getInnerData().getPartId() != supplierPartById.getInnerData().getPartId()
				&& supplierPart.getInnerData().getSupplierId() != supplierPartById.getInnerData().getSupplierId()) {
			SupplierPart supplierPartData = supplierPartDataHandler.getCustomerPartBySupplierIdAndPartId(
					supplierPart.getInnerData().getSupplierId(), supplierPart.getInnerData().getPartId());
			if (supplierPartData.getInnerData() != null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("修改后的供应商物料关系已存在，不可进行修改!");
				return resultBean;
			}
		}
		supplierPartDataHandler.update(supplierPart);
		resultBean.setSuccess(true);
		resultBean.setMessage("供应商物料关系修改成功!");
		return resultBean;
	}

//	@Override
	public SupplierPart getById(long id) {
		return supplierPartDataHandler.getById(id);
		}

//	@Override
	public SupplierPart getByName(String name) {
		return supplierPartDataHandler.getByName(name);
	}

	@Override
	public List<SupplierPart> getSupplierPartBySupplierId(long supplierId) {
		return supplierPartDataHandler.getSupplierPartBySupplierId(supplierId);
	}

	@Override
	public List<SupplierPart> getSupplierPartByPartId(long partId) {
		return supplierPartDataHandler.getSupplierPartByPartId(partId);
	}
}
